package cn.git.bbs.core.controller;

import cn.git.bbs.core.bean.product.Brand;
import cn.git.bbs.core.service.brand.BrandService;
import cn.git.bbs.core.service.solr.SearchService;
import cn.itcast.common.page.Pagination;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.*;

@Controller
public class SearchController {

    @Autowired
    private SearchService searchService;
    @Autowired
    private BrandService brandService;
    @RequestMapping(value = "/search")
    public String search(Model model, Integer pageNo, String keyword,Long brandId,String price) throws Exception {

        List<Brand> brands = brandService.selectBrandListByRedis();

        Map<String,String> map = new HashMap<String, String>();
        if(null != brandId){
            Brand brand = brandService.selectBrandById(brandId);
            map.put("品牌:",brand.getName());
        }
        if(null != price){
            if(price.contains("-")){
                map.put("价格:",price);
            }else {
                map.put("价格:",price + "以上");
            }
        }
        model.addAttribute("map",map);

        Pagination pagination = searchService.selectPaginationByQuery(keyword, pageNo,brandId,price);
        model.addAttribute("pagination",pagination);
        model.addAttribute("brands",brands);
        return "search";
    }
}
